﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
using PH.Model.Results;
using Newtonsoft.Json.Linq;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("Class")]
    public class ClassApiController : ApiController
    {
        [Route("GetClasses")]
        [HttpPost]
        public JsonEntitySet<ClassModel> GetClasses(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ClassQuery search = new ClassQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                var schGuid = request.Value<string>("schoolGuid");
                if (!string.IsNullOrEmpty(schGuid))
                    search.SchoolGuid = new Guid(schGuid);
                search.Key = request.Value<string>("key");
                search.GetPersonCount = request.Value<bool>("getPersonCount");
                search.GetGradeName = request.Value<bool>("getGradeName");
                var result = ClassLogic.GetClasses(search);
                return new JsonEntitySet<ClassModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ClassModel>(null, errinfo, false);
        }

        [Route("SaveClass")]
        [HttpPost]
        public JsonEntity<ClassModel> SaveClass(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                ClassModel cla = new ClassModel();
                var guid = request.Value<string>("guid");
                var schGuid = request.Value<string>("schoolGuid");
                cla.Guid = new Guid(guid);
                cla.SchoolGuid = new Guid(schGuid);
                cla.GradeCode = request.Value<int>("gradeCode");
                cla.Advisor = request.Value<string>("advisor");
                cla.ClassName = request.Value<string>("className");
                cla.CIndex = request.Value<int>("cindex");
                #endregion

                if (cla.Guid == Guid.Empty)
                    cla = ClassLogic.CreateNewClass(cla);
                else
                    ClassLogic.UpdateClass(cla);
                return new JsonEntity<ClassModel>(cla);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<ClassModel>(null, errinfo, false);
        }

        [Route("DeleteClass")]
        [HttpPost]
        public JsonResult<bool> DeleteClass(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guid = request.Value<string>("guid");
                #endregion

                ClassLogic.DeleteClass(new Guid(guid));
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("ImportClass")]
        [HttpPost]
        public JsonResult<bool> ImportClass(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var fGuid = request.Value<string>("fGuid");
                var sGuid = request.Value<string>("sGuid");
                if (string.IsNullOrEmpty(fGuid))
                    throw new Utils.PhException("没有找到上传的文件");
                if (string.IsNullOrEmpty(sGuid))
                    throw new Utils.PhException("无效的学校编号");
                var eGuid = ClassLogic.ImportClass(new Guid(fGuid), new Guid(sGuid));
                if (eGuid == Guid.Empty)
                    return new JsonResult<bool>(true);

                errinfo = string.Format("导入班级数据失败，请下载结果文件查看详情。<div style='text-align:center; padding:10px'><a href='/Files/DownLoad/{0}' target='_blank'>详情请点击下载Excel文件</a></div>", eGuid);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("CreateForignClass")]
        [HttpPost]
        public JsonResult<bool> CreateForignClass(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guid = request.Value<string>("schGuid");
                #endregion

                ClassLogic.CreateForignClass(new Guid(guid));
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
